<?php
error_reporting(0);
$id=$_GET['id']; 

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("morganse_crm") or die(mysql_error());
$array=array();


// App Info array['app']['xxx']
$sql="SELECT * FROM crm_application WHERE comesfrom='MaxFinance' AND IdenticalID =".$id;
$result = mysql_query($sql) or die(mysql_error());
$row = MYSQL_FETCH_ASSOC($result);
FOREACH ($row as $key=>$value){
	$array['app'][$key]=$value;
}


// Client1&2 info array['client1']['xxx'] , array['client2']['xxx']
$sql="SELECT * FROM crm_contact WHERE comesfrom='MaxFinance' AND app_id =".$id;
$result = mysql_query($sql) or die(mysql_error());
$i='client1';
WHILE($row = MYSQL_FETCH_ASSOC($result))// put client fields into array
{
	FOREACH($row AS $key=>$value)
		$array[$i][$key]=$value;
	$i='client2';
}


// Employment1&2 Info array['emp1']['xxx'],array['emp2']['xxx']
$sql="SELECT * FROM crm_employment WHERE Comesfrom='MaxFinance' AND IdenticalID =".$id;
$result = mysql_query($sql) or die(mysql_error());
$i='emp1';
WHILE($row = MYSQL_FETCH_ASSOC($result))// put employment fields into array
{
	FOREACH($row AS $key=>$value)
		$array[$i][$key]=$value;
	$i='emp2';
}



// Credit History 1&2 info array['credit1']['xxx'] , array['credit2']['xxx']
$sql="SELECT * FROM crm_applicationcredit WHERE comesfrom='MaxFinance' AND IdenticalID =".$id;
$result = mysql_query($sql) or die(mysql_error());
$i='credit1';
WHILE($row = MYSQL_FETCH_ASSOC($result))// put credit fields into array
{
	FOREACH($row AS $key=>$value)
		$array[$i][$key]=$value;
	$i='credit2';
}



$homeaddress1= $array['client1']['StreetNo']." ".$array['client1']['Street']." ".$array['client1']['Suburb']." ".$array['client1']['State']." ".$array['client1']['PostCode'];
$homeaddress2= $array['client2']['StreetNo']." ".$array['client2']['Street']." ".$array['client2']['Suburb']." ".$array['client2']['State']." ".$array['client2']['PostCode'];


// Income array
$sql="SELECT * FROM crm_income WHERE Comesfrom='MaxFinance' AND IdenticalID =".$id;
$result = mysql_query($sql) or die(mysql_error());
$i=1;
WHILE($row = MYSQL_FETCH_ASSOC($result))// put income fields into array
{
	FOREACH($row AS $key=>$cell)
		$arrayincome[$i][$key]=$cell;
	$i++;
}
$wincome1 = $arrayincome[1]['income_aftertax']; //echo $wincome1;
$wincome2 = $arrayincome[2]['income_aftertax'];

$wincome1p = $arrayincome[1]['income_aftertax_period'];
$wincome2p = $arrayincome[2]['income_aftertax_period'];

$cincome1 = $arrayincome[1]['income_centrelink'];
$cincome2 = $arrayincome[2]['income_centrelink'];

$oincome1 = $arrayincome[1]['income_other'];
$oincome2 = $arrayincome[2]['income_other'];

$oincome1p = $arrayincome[1]['income_other_period'];
$oincome2p = $arrayincome[2]['income_other_period'];

$oincome1spec = $arrayincome[1]['OtherIncomeSpecify'];
$oincome2spec = $arrayincome[2]['OtherIncomeSpecify'];
?>

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>General Form View</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link href="http://maxfunding.com.au/bootstrap/css/bootstrap.css" rel="stylesheet">
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>
<script src="form-view-js.js"></script>
<script src="jquery.formatDateTime.min.js"></script>
<script src="jquery.formatCurrency-1.4.0.min.js"></script>
<link rel="stylesheet" type="text/css" href="form-vew-style.css">
<style type="text/css">
#cal-table tr td {
	border: none;
	border-bottom: 1px solid #ccc;
	border-top:1px solid #ccc;"
}
</style>
</head>

<body>
<div class="container">
  <table class="no-border">
    <tr>
      <td width="60%" style="padding-left:0"><?php echo "<b><h3>".$array['client1']['SurName'].", ".$array['client1']['FirstName']."</h3></b>"; ?></td>
      <td class="text-right"><?php echo "<b>ID: </b>".$array['app']['app_id']; ?></td>
      <td class="text-right"><?php echo " <b>Submitted: </b>".$array['app']['CreatedDate']." ".$array['app']['CreatedTime']; ?></td>
    </tr>
  </table>
  <div id="joint" style="color:red;">
    <?php 
if($array['app']['prevclient']!='')
echo "Joint Application with ".$array['app']['prevclient']; ?>
  </div>
  <div id="notice" style="background:#f3f3f3; padding:5px;"><?php echo $array['app']['formviewnotice']; ?></div>
  <br>
  <h4>General Form - <span class="color-font"><?php echo $array['app']['mc_final_status']; ?></span></h4>
  <table border="1" class="yellow-bg">
    <tr class="yellow-bg">
      <td width="33%">Desired Amount</td>
      <td class="currency"><?php echo $array['app']['DesiredAmount']; ?></td>
    </tr>
    <tr>
      <td>Asset</td>
      <td><?php echo $array['app']['Assets']; ?></td>
    </tr>
  </table>
  <br>
  <table class="no-border big-font-table">
    <tr>
      <td width="33%"><a href="#income">Disposable Income</a></td>
      <td class=""><span class="color-font"><?php echo $array['app']['mc_income_status']; ?></span></td>
    </tr>
    <tr>
      <td width="33%"><a href="#asset">Asset</a></td>
      <td class=""><span class="color-font"><?php echo $array['app']['mc_asset_status']; ?></span></td>
    </tr>
    <tr>
      <td><a href="#credit">Credit</a></td>
      <td><span class="color-font"><?php echo $array['app']['mc_credit_status']; ?></span></td>
    </tr>
  </table>
  <br>
  <hr />
  <h4>Personal Details</h4>
  <table id="personal-table">
    <tr>
      <td width="33%">Title</td>
      <td width="33%"><?php echo $array['client1']['Title']?></td>
      <td id="title2"><?php echo $array['client2']['Title']?></td>
    </tr>
    <tr class="yellow-bg">
      <td>Full Name</td>
      <td><?php echo $array['client1']['FirstName']." ".$array['client1']['MiddleName']." ".$array['client1']['SurName']?></td>
      <td><?php echo $array['client2']['FirstName']." ".$array['client2']['MiddleName']." ".$array['client2']['SurName']?></td>
    </tr>
    <tr class="yellow-bg">
      <td>Citizenship</td>
      <td><?php echo $array['client1']['Citizenship']?></td>
      <td><?php echo $array['client2']['Citizenship']?></td>
    </tr>
    <tr class="yellow-bg">
      <td>Mobile</td>
      <td><?php echo $array['client1']['Mobile']?></td>
      <td><?php echo $array['client2']['Mobile']?></td>
    </tr>
    <tr class="yellow-bg">
      <td>Home Phone</td>
      <td><?php echo $array['client1']['HomePhone']?></td>
      <td><?php echo $array['client2']['HomePhone']?></td>
    </tr>
    <tr class="yellow-bg">
      <td>Email</td>
      <td><?php echo $array['client1']['Email']?></td>
      <td><?php echo $array['client2']['Email']?></td>
    </tr>
    <tr class="yellow-bg">
      <td>Home Address</td>
      <td><?php echo "<a class='address'>".$homeaddress1."</a>" ?></td>
      <td><?php echo "<a class='address'>".$homeaddress2."</a>" ?></td>
    </tr>
   	<tr class="yellow-bg">
    	<td>Date of Birth</td>
    	<td><span id="dob1"><?php echo $array['client1']['DateBirth']?></span> <span id="age1"></span>  </td>
    	<td><span id="dob2"><?php echo $array['client2']['DateBirth']?></span> <span id="age2"></span>  </td>
  	</tr>
    <tr>
      <td>Driver License</td>
      <td><?php echo $array['client1']['DriverLicense']?></td>
      <td><?php echo $array['client2']['DriverLicense']?></td>
    </tr>
    <tr>
      <td>Residential Status</td>
      <td><?php echo $array['client1']['app_ResidentialStatus']?></td>
      <td><?php echo $array['client2']['app_ResidentialStatus']?></td>
    </tr>
    <tr>
      <td>Number of Dependant</td>
      <td id="nod"><?php echo $array['client1']['Dependant']?></td>
      <td><?php echo $array['client2']['Dependant']?></td>
    </tr>
  </table>
  <br>
  <h5><strong>Purpose</strong></h5>
  <table>
    <tr>
      <td width="33%">Purpose of Loan</td>
      <td><?php echo $array['app']['loanpurposereason']?></td>
    </tr>
    <tr>
      <td>Purpose Detail</td>
      <td><?php echo $array['app']['purposedetail']?></td>
    </tr>
  </table>
  <hr />
  <h4 id="income">Disposable Income - <span class="color-font"><?php echo $array['app']['mc_income_status']; ?></span></h4>
  <br>
  <h5><strong>Serviceablility Calculator (Monthly)</strong></h5>
  <br>
  <table id="cal-table" width="100%" cellpadding="0" cellspacing="0" >
      <tr>
        <td width="15%"><strong>Total Income</strong><br>
          (Min: $2166)</td>
        <td width="15%"><strong  class="currency"><?php echo $array['app']['totalincome']; ?></strong></td>
        <td width="5%"><strong> - </strong></td>
        <td width="20%"><strong>Total Expenses</strong></td>
        <td width="15%" ><strong class="currency"><?php echo $array['app']['totalincome']-$array['app']['disposableincome']; ?></strong></td>
        <td width="5%"><strong> = </strong></td>
        <td width="15%"><strong>Disposable Income</strong><br>
          (Min: $400)</td>
        <td width=""  ><strong class="currency"><?php echo $array['app']['disposableincome']; ?></strong></td>
      </tr>
      <tr>
        <td>Work Income</td>
        <td><?php echo $wincome1.' '.$wincome1p.'<br>'.$wincome2.' '.$wincome2p; ?></td>
        <td>&nbsp;</td>
        <td class="declared">Declared Living expense</td>
        <td class="declared" id="declared"><?php echo $array['app']['livingfee']; ?></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>Centrelink(fortnightly)</td>
        <td><?php echo $cincome1.'<br>'.$cincome2; ?></td>
        <td>&nbsp;</td>
        <td class="standard">Standard Living expense</td>
        <td class="standard" id="standard"></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>Other Income</td>
        <td><?php echo $oincome1.' '.$oincome1p;
				echo $oincome1spec.'<br>';
				echo $oincome2.' '.$oincome2p;
				echo $oincome2spec;
	 ?></td>
        <td>&nbsp;</td>
        <td class="boarding">Boarding/Renting fee</td>
        <td class="boarding"><?php echo $array['app']['boardingfee']; ?></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>Credit Commitment</td>
        <td><?php echo $array['app']['totalexpensecorrect']; ?></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table><br>
    
  <h5><strong>Employment / Company / Income Details</strong></h5>
    
    <table>
  <tr>
    <td width="33%">Employment Status</td>
    <td width="33%"><?php echo $array['emp1']['employment_status']; ?></td>
    <td><?php echo $array['emp2']['employment_status']; ?></td>
  </tr>
  <tr>
    <td>Type of Employment</td>
    <td><?php echo $array['emp1']['employment_type']; ?></td>
    <td><?php echo $array['emp2']['employment_type']; ?></td>
  </tr>
  <tr>
    <td>Employer Name</td>
    <td><?php echo $array['emp1']['employer_name']; ?></td>
    <td><?php echo $array['emp2']['employer_name']; ?></td>
  </tr>
  <tr>
    <td>Title in Occupation</td>
    <td><?php echo $array['emp1']['employment_occupation']; ?></td>
    <td><?php echo $array['emp2']['employment_occupation']; ?></td>
  </tr>
  <tr>
    <td>Work Phone for Contact Person</td>
    <td><?php echo $array['emp1']['employer_phone']; ?></td>
    <td><?php echo $array['emp2']['employer_phone']; ?></td>
  </tr>
</table>



    
    
  <hr />
  
  
  <h4 id="asset">Asset - <span class="color-font"><?php echo $array['app']['mc_asset_status']; ?></span></h4>
  <?php
// vehicle table
$sql="SELECT * FROM crm_vehicle WHERE Comefrom='MaxFinance' AND IdenticalID =".$id;
$result = mysql_query($sql) or die(mysql_error());
echo "<table class='vehicle-table'>";
ECHO "<tr>";
$fields_num = MYSQL_NUM_FIELDS($result);
FOR($i=0; $i<$fields_num; $i++)
{
	$field = MYSQL_FETCH_FIELD($result);
	ECHO "<td width='25%'>{$field->name}</td>";
}
ECHO "</tr>\n";

WHILE($row = MYSQL_FETCH_ASSOC($result))
{
	
	ECHO "<tr>";

	// $row is array... foreach( .. ) puts every element
	// of $row to $cell variable
	FOREACH($row AS $key=>$cell){
		if($key=="VehicleValue")
			ECHO "<td class='currency'>$cell</td>";
		else
			ECHO "<td>$cell</td>";
	}
	ECHO "</tr>\n";
}
echo "</table>";
// vehicle table END








// 2 property tables
$sql="SELECT * FROM crm_property WHERE Comefrom='MaxFinance' AND IdenticalID =".$id;
$result = mysql_query($sql) or die(mysql_error());

$i=1;
$col='property'.$i;
WHILE($row = MYSQL_FETCH_ASSOC($result))
{
	FOREACH($row AS $key=>$value)
		$array[$col][$key]=$value;
	$i++;
	$col='property'.$i;
}

// property short table
echo '
	<table class="property-short-table">
		<tr style="background:#d9edf7">
			<td>Property</td>
			<td>Current Value</td>
			<td>Amount Owing</td>
			<td>LVR%</td>
			<td>Bank/Financier</td>
			<td>Owners Name</td>
		</tr>
';
for($j=1;$j<=6;$j++){
	if($array["property".$j]["PropertyValue"]!=''){
		echo '
			<tr>
				<td>Real Estate '.$j.'</td>
				<td class="currency" id=propertyValue'.$j.'>'.$array["property".$j]["PropertyValue"].'</td>
				<td class="currency" id=propertyOwing'.$j.'>'.$array["property".$j]["MortgageOwing"].'</td>
				<td id=LVR'.$j.'></td>
				<td>'.$array["property".$j]["MortgageLender"].'</td>
				<td>'.$array["property".$j]["Owner"].'</td>
			</tr>
		';
	}
}
echo '</table>';
// property short table END


// Liability Info
$sql="SELECT * FROM crm_liabilities WHERE Comesfrom='MaxFinance' AND IdenticalID =".$id;
$result = mysql_query($sql) or die(mysql_error());
echo "<table class='liability-table'><tr>";
$fields_num = MYSQL_NUM_FIELDS($result);
FOR($i=0; $i<$fields_num; $i++)
{
	$field = MYSQL_FETCH_FIELD($result);
	ECHO "<td>{$field->name}</td>";
}echo "</tr>";
WHILE($row = mysql_fetch_assoc($result))
{
	ECHO "<tr>";
	FOREACH($row AS $key=>$cell)
		echo "<td width='25%'>".$cell."</td>";
	ECHO "</tr>";
}
echo "</table>";
// Liability Info END


// property details table
for($j=1;$j<=5;$j+=2){
	$k=$j+1;
	echo '
		<table class="property-table">
			<tr style="background:#d9edf7">
				<td width="33%"><strong></strong></td>
				<td width="33%"><strong>Property '.$j.'</strong></td>
				<td><strong>Property '.$k.'</strong></td>
			</tr>
							
			<tr>
				<td>Address</td>
				<td><a class="address">'.$array["property".$j]["Address"].'</a></td>
				<td><a class="address">'.$array["property".$k]["Address"].'</a></td>
			</tr>
			
			<tr>
				<td>Arrears</td>
				<td>'.$array["property".$j]["Arrears"].'</td>
				<td>'.$array["property".$k]["Arrears"].'</td>
			</tr>
		</table>
	';
}
// property details table END
?>
  <hr />
  <h4 id="credit">Credit - <span class="color-font"><?php echo $array['app']['mc_credit_status']; ?></span></h4>
  <table id="credit-table">
    <tr>
      <td width="33%">Have  you ever applied for bankruptcy or hardship?</td>
      <td width="33%"><?php echo $array['credit1']['credit_hardship']?></td>
      <td><?php echo $array['credit2']['credit_hardship']?></td>
    </tr>
    <tr>
      <td>Your credit history?</td>
      <td><?php echo $array['credit1']['credit_history']?></td>
      <td><?php echo $array['credit2']['credit_history']?></td>
    </tr>
    <tr>
      <td>Total Outstanding Value </td>
      <td><?php echo $array['credit1']['credit_outstanding']?></td>
      <td><?php echo $array['credit2']['credit_outstanding']?></td>
    </tr>
    <tr>
      <td>Discharge Date </td>
      <td><?php echo $array['credit1']['credit_dischargedate']?></td>
      <td><?php echo $array['credit2']['credit_dischargedate']?></td>
    </tr>
    <tr>
      <td>Discharge </td>
      <td><?php echo $array['credit1']['credit_discharge']?></td>
      <td><?php echo $array['credit2']['credit_discharge']?></td>
    </tr>
  </table>
  <hr />
  <h4>Others</h4>
  <table>
    <tr>
      <td width="33.3%"><strong>Time cost:</strong> <?php echo $array['app']['timecost'].' mins'; ?></td>
      <td width="33.3%"><strong>IP:</strong> <?php echo $array['app']['mc_ipaddress']; ?></td>
      <td><strong>Preapp Code: </strong><?php echo $array['app']['preappcode']; ?></td>
    </tr>
    <tr>
      <td colspan="3"><strong>Browser:</strong> <?php echo $array['app']['mc_browser']; ?></td>
    </tr>
    <tr>
      <td colspan="3"><strong>Afterpage:</strong> <a id="afterpage"><?php echo $array['app']['afterpage']; ?></a></td>
    </tr>
    <tr>
      <td colspan="3"><strong>Came From:</strong> <?php echo $array['app']['comesfrom']; ?></a></td>
    </tr>
  </table>
</div>

<!-- end .container --> 

<script>


</script>
</body>
</html>